;(function($){
	$.fn.spa_use_coupon = function(opts){
		var el = this;
		var opt = {
			mno : "66",
			arg : "",
			urlPre : "/qms/mobile/",//区分不同app
			dataBack : function(){},
			closeBack : function(){},
		}
		var param = $.extend(opt,opts);
		var app = new App(el,param);
	}
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		this.initHtml();
		var t = this;
		if(param.mno){
			this.getMarBymno(function(ds){
				t.getMarInfo(ds);
			});
		}else{
			this.getMarInfo({});
		}
		this.initCouponHtml();
		this.bindEvent();
	}
	
	App.prototype = {
		bindEvent : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			//关闭，返回
			el.find("#close-dialog,#modalBtnClose").unbind("click").click(function(){
				el.modal("hide");
				el.empty();
				param.closeBack();
			});
			
			//查找会员
			el.find("#mno_help,#modalBtnSearch").unbind("click").click(function(){
				$(this).modalFrame("destroy");
				$(this).modalFrame("init",function(modalEle){
					el.modal("toggle");
					$("#"+modalEle).modal("show"); 
					if(!$.isFunction($().sel_marcard)){
						$.ajaxSetup({ async: false, cache: false });
			            $.getScript("js/cashier_mana/jquery.modal.sel_marcard.js");
			            $.ajaxSetup({ async: true });
					}
					$("#"+modalEle).sel_marcard({//selection_membershipCard	
						oper : "s", //o:进行数据保存, s:返回选择积分卡数据;
	 					filterSearch : $("#mname_input").val(),
						dataBack:function(data){
							$(modalEle).modal("hide");
							$(modalEle).empty();
							el.modal("toggle");
							t.getMarInfo(data);
							param.mno = data.mno;
							$.coupon_got_info.param.mno = data.mno;
							$.coupon_got_info.coupon_gotDefault();
//							initPageHtml(data.mno);
						},
						closeBack:function(){
							$(modalEle).modal("hide");
							el.modal("toggle");
							$(modalEle).empty();
						}
					});
				});
			});
		},
		initHtml : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			el.addClass("modal").attr("tabindex","-2").attr("data-backdrop","static");
			var html = 	'<div class="modal-dialog" style="width:1132px;">'+
							'<div id="pageOverlay" class="hide"></div>'+
							'<div id="loading" class="loading hide">'+
								'<i class="ace-icon fa fa-spinner fa-spin orange bigger-250"></i>'+
							'</div>'+
							'<div class="modal-content">'+
								'<div class="modal-header no-padding">'+
									'<div class="table-header">'+
										'<button id="close-dialog" type="button" class="close" data-dismiss="modal" aria-hidden="true" value="HTML">'+
											'<span class="white">&times;</span>'+
										'</button>'+
										'<span id="modal-h4">'+$.i18n.propJsp("使用优惠券")+'</span>'+
									'</div>'+
								'</div>'+
								'<div class="modal-body">'+
									'<div class="well well-sm hide">'+
										'<div class="clearfix">'+
												
											'<form id="frmTktInfo" class="form-horizontal" onsubmit="return false">'+
												'<div class="col-sm-3" style="width:240px;">'+
													
													'<div class="form-group">'+
														'<label class="col-md-3 control-label" data-i18n="会员:">'+$.i18n.propJsp("会员")+'</label>'+
														'<div class="col-md-9">'+
															'<input type="text" data-i18n-placeholder="卡号/名称/电话" placeholder="卡号/名称/电话" class="input-medium" name="mname_input" id="mname_input">'+
															'<i id="mno_help" class="ace-icon fa fa-caret-down bigger-150" data-arg="" style="cursor:pointer;margin-left:-18px;"></i>'+
													    '</div>'+
													'</div>'+
												'</div>'+
												'<div class="col-sm-6">'+
													'<div class="btn-group">'+
														'<button id="modalBtnSearch" class="btn btn-sm btn-info" data-i18n="查询">'+
															'<i class="ace-icon fa fa-search"></i>'+
															$.i18n.propJsp("查询")+
														'</button>'+
													'</div>'+
												'</div>'+
											'</form>'+
											'<div class="pull-right">'+
												'<div class="btn-group hide">'+
													'<button id="btnFresh" class="btn btn-sm btn-info" data-i18n="刷新">'+
													'</button>'+
												'</div>'+
												'<div class="btn-group">'+
													'<button id="modalBtnClose" class="btn btn-sm btn-default" data-i18n="关闭">'+
														'<i class="ace-icon fa fa-times"></i>'+
														$.i18n.propJsp("关闭")+
													'</button>'+
												'</div>'+
											'</div>'+
										'</div>'+
									'</div>'+
									'<div id="marInfo" class="widget-box row hide" style="height:72px;">'+
									
									'</div>'+
									'<div class="coupon-main">'+
										'<div id="couponContainer_got" class="coupon-scroll" style="width:1110px;height:400px;">'+
                                            
                                        '</div>'+
                                    '</div>'+
								'</div>'+
								'<div class="modal-footer">'+
//									'<div class="btn-group">'+
//										'<button id="modalBtnCheck" type="button" class="btn btn-sm btn-info">'+
//											'<i class="ace-icon fa fa-check"></i>'+$.i18n.propJsp("使用")+
//										'</button>'+
//									'</div>'+
									'<div class="btn-group">'+
										'<button id="modalBtnClose" type="button" class="btn btn-sm btn-default">'+
											'<i class="ace-icon fa fa-undo"></i>'+$.i18n.propJsp("返回")+
										'</button>'+
									'</div>'+
								'</div>'+
							'</div>'+
						'</div>';
			this.el.html(html);
		},
		getMarInfo:function(ds){//会员信息
			var el = this.el;
			var param = this.param;
			var t = this;
			if(!$.isFunction($().marcard_info)){
				$.ajaxSetup({ async: false, cache: false });
	            $.getScript("js/cashier_mana/jquery.service.marcard_info.js");
	            $.ajaxSetup({ async: true });
			}
			$("#marInfo").marcard_info({
				ds : ds,
			})
		},
		initCouponHtml : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			if(!$.isFunction($().coupon_got_info)){
				$.ajaxSetup({ async: false, cache: false });
	            $.getScript("js/cashier_mana/jquery.service.coupon_got_info.js");
	            $.ajaxSetup({ async: true });
			}
			el.find("#couponContainer_got").coupon_got_info({
				wait : function(){},
				goon : function(){},
				mno : param.mno,
				width : el.find("#couponContainer_got").width(),
				dataBack : function(){},
				closeBack : function(){},
				useBack : function(js){
					t.useCoupon(js.couponid);
				}
			})
		},
		useCoupon : function(couponid,paramer){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			//构造参数
    		var pa={};
    		if(paramer){
    			pa.arg={};
    			pa.arg=$.extend({},pa.arg,paramer); 
    			t.wait();
    			$.post($.HSAPI_AJAXURL+param.urlPre+"procedure2",  
					  $.HSAPI_ASKJSONINFO(JSON.stringify(pa),'HoMemberCoupon_Active','{}'),   
					  function(data){
        				t.goon();
    					if(data.RETURN==1){ 
							el.modal("hide");
							el.empty();
    						param.dataBack();
    					}else if(data.RETURN=="-10"){ 
    						t.errorContent(pa.arg,data,couponid);  
    					}else if(data.RETURN=="-11"){ 
    						t.errorContent_11(pa.arg,data,couponid);  
    					}else{
    						jf_alert(data.MSG);
    					}
    			},"json"); 
    		}else{
    			pa.arg={arg:param.arg,operation:"actived"};
    			if(param.arg=="rm"){
    				pa.arg.gno = param.gno
    			}else if(param.arg=="pos"){
    				pa.arg.no = param.no
    			}
        		pa.arg=$.extend({},pa.arg,{"couponid":couponid,mno:param.mno});//,"remark":el.find("#remark").val()   
        		if(couponid){
        			//二维码
        			$("#smscode").modalFrame("destroy");
        			$("#smscode").modalFrame("init",function(modalEle){
        				if(!$.isFunction($().smscode)){
        					$.ajaxSetup({ async: false, cache: false });
        		            $.getScript("js/cashier_mana/jquery.modal.smscode.js");
        		            $.ajaxSetup({ async: true });
        				}
        		    	$("#"+modalEle).smscode({
        		    		couponid:couponid,
        		    		dataBack : function(data){
        		    			$("#"+modalEle).modal("hide");
        		    			$("#"+modalEle).empty();
        		    			pa.arg["smscode"]=data;
								t.useCoupon(couponid,pa.arg)
        		    		},
        		    		returnBack : function(){
        		    			$("#"+modalEle).modal("hide");
        		    			$("#"+modalEle).empty();
        		    			t.goon();
        		    		},
        		    	});
        			})
        		}else{
        			jf_alert($.i18n.propJsp("请选择一张电子券")+"！")
        		}
    		}
		},
		//出错提示
    	errorContent : function(pa,data,couponid){
    		var t=this;
    		var param=this.param; 
    		var el=this.el;  
    		
    		var MSG = data.MSG;
    		var name = data.checkbox;
    		
    		
    		var html = 	'<h3 class="header smaller lighter blue">'+
    						'<i class="ace-icon fa fa-bullhorn"></i>'+MSG+
    					'</h3>'+
    					'<div class="alert alert-block alert-danger">'+
    						'<label>'+
    							'<input id="checkin_cccheckbox" type="checkbox" name="'+name+'" value="" class="ace"/>'+ 
    							'<span class="lbl" style="color:#478fca;">'+MSG+'</span>'+
    						'</label>'+
    					'</div>';

    		bootbox.dialog({
    			message: html,
    			buttons:{
    				"button1":{
    					"label" : $.i18n.propJsp("确定"),
    					"className" : "btn-sm btn-info",
    					"callback" : function(){
    						if($("#checkin_cccheckbox").prop("checked")){
    							pa[name]="yes";
    						}else{
    							pa[name]="no";
    						}
							t.useCoupon(couponid,pa.arg)  
    					}
    				},	
    				"button" :
    				{
    					"label" : $.i18n.propJsp("取消"),
    					"className" : "btn-sm",
    				},
    			}
    		});
    	},
    	//出错提示-11
    	errorContent_11 : function(pa,data,couponid){
    		var t=this;
    		var param=this.param; 
    		var el=this.el;  
    		
    		var name = data.radiobutton
    		
    		$("#save").modalFrame("destroy");
			$("#save").modalFrame("init",function(modalEle){
				if(!$.isFunction($().RETURN_11)){
					$.ajaxSetup({ async: false, cache: false });
		            $.getScript("js/cashier_mana/jquery.RETURN_11.js");
		            $.ajaxSetup({ async: true });
				}
		    	$("#"+modalEle).RETURN_11({
		    		data:data,
		    		dataBack : function(data){
		    			$("#"+modalEle).modal("hide");
		    			$("#"+modalEle).empty();
		    			pa[name]=data;
						t.useCoupon(couponid,pa.arg);    
		    		},
		    		returnBack : function(){
		    			$("#"+modalEle).modal("hide");
		    			$("#"+modalEle).empty();
		    			t.useCoupon(couponid,pa.arg);  
		    		},
		    	});
			})
    	},
		getMarBymno : function(back){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var arg = {"arg":{"mno":param.mno}};
			jQuery.ajax({
				type :'POST',  
				url : $.HSAPI_AJAXURL + param.urlPre+"procedure2",  
				data : $.HSAPI_ASKJSONINFO( JSON.stringify(arg),'HoMemberRetrieve','{}'),
				dataType : 'json',
				success : function(data) {
					if(data.RETURN==1){
						back(data.ho_marcust_member[0])
					}
				},  
				error : function(data) {  
					jf_alert(data);
				}  
			});
			
		},
		goon : function(){
			var el = this.el;
			el.find("#loading").addClass("hide");
			el.find("#pageOverlay").addClass("hide");
		},
		wait : function(){
			var el = this.el;
			el.find("#loading").removeClass("hide");
			el.find("#pageOverlay").removeClass("hide");
		},
	}
	
	function sortByKey(array, key) {
		return array.sort(function(a, b) {
			var x = parseInt(a[key]);
			var y = parseInt(b[key]);
			if(x==0){
				x = 9999;
			}
			if(y==0){
				y = 9999;
			}
			return ((x < y) ? -1 : ((x > y) ? 1 : 0));
		});
	}
	
})(jQuery);